package org.tgcms.core.samples.appengine;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.tgcms.core.dal.appengine.dao.AppenginePageDao;
import org.tgcms.core.dal.appengine.dao.AppengineRedirectDao;
import org.tgcms.core.dal.appengine.dao.AppengineTemplateDao;
import org.tgcms.core.dal.appengine.dao.AppengineWebsiteDao;
import org.tgcms.core.dal.appengine.domain.AppengineRedirect;
import org.tgcms.core.dal.appengine.domain.AppengineWebsite;
import org.tgcms.core.dal.dao.DaoException;
import org.tgcms.core.dal.dao.PageDao;
import org.tgcms.core.dal.dao.RedirectDao;
import org.tgcms.core.dal.dao.TemplateDao;
import org.tgcms.core.dal.dao.WebsiteDao;
import org.tgcms.core.dal.dto.Redirect;
import org.tgcms.core.dal.dto.Website;
import org.tgcms.core.samples.contentcreator.ContentCreator;

public class RouterServlet extends HttpServlet {
	
	WebsiteDao websiteDao = new AppengineWebsiteDao();
	TemplateDao templateDao = new AppengineTemplateDao();
	PageDao pageDao = new AppenginePageDao();
	RedirectDao redirectDao = new AppengineRedirectDao();

	private static final long serialVersionUID = -1999885731327138730L;

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		boolean createContent = "createContent=true".equals(req.getQueryString());

		resp.setContentType("text/plain; charset=utf-8");
		
		resp.getWriter().println("test");
		
		if(createContent) {
			resp.getWriter().println("cc");
			ContentCreator cc = new ContentCreator(websiteDao, templateDao, pageDao); 
			try {
				cc.createSampleContent();
			} catch (DaoException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		String uriString = req.getRequestURI().toString();

		String url = uriString.replaceAll("/$", "").replaceAll("^/", "");
		if (url.isEmpty()) {
			url = "index.html";
		}
		
		resp.getWriter().println("req.getServerName(): "+req.getServerName());
		

		String fullUrl = (req.getQueryString() != null) ? (req.getRequestURI() + "?" + req
				.getQueryString()) : req.getRequestURI();

		
		try {
			resp.getWriter().println(" websites:");
			List<Website> websites = websiteDao.listAll();
			for(Website website : websites) {
				resp.getWriter().println(" website: "+website.getName());
			}
			if(websites.size() == 0) {
				AppengineWebsite neww = new AppengineWebsite();
				neww.setName("default website");
				websiteDao.put(neww);
			}
			resp.getWriter().println(" redirects:");
			List<Redirect> redirects = redirectDao.listAll();
			for(Redirect redirect : redirects) {
				resp.getWriter().println(" redirect: "+redirect.getName());
			}
			if(websites.size() > 0) {
				Redirect newr = redirectDao.create(websites.get(0));
				newr.setName("r"+System.currentTimeMillis());
				redirectDao.put(newr);
			}
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		if (redirects != null) {
//			for (Redirect redirect : redirects) {
//				if (redirect.getFromUrl() != null && 
//						redirect.getToUrl() != null &&
//						fullUrl.matches(redirect.getFromUrl())) {
//					Pattern pattern = Pattern.compile(redirect.getFromUrl());
//					Matcher matcher = pattern.matcher(fullUrl);
//					String redirectUrl = redirect.getToUrl();
//					if (matcher.find()) {
//						for (int i = 0; i < matcher.groupCount(); i++) {
//							redirectUrl = redirectUrl.replace("{" + i + "}", matcher.group(i + 1));
//						}
//					}
//					resp.sendRedirect(redirectUrl);
//					return;
//				}
//			}
//		}
//
//		Page page = pageDao.findByUrl(url);
//		
//		if(debug) {
//			resp.getWriter().println("Url: "+url);
//			resp.getWriter().println("Page: "+page);
//			if(page != null) {
//				resp.getWriter().println("Page.getTemplate(): "+page.getTemplate());
//			}
//		}
//
//		if (page == null || page.getTemplate() == null) {
//			AttachmentDao attachmentDao = new AppengineAttachmentDao();
//			Attachment attachment = attachmentDao.findByUrl(url);
//
//			if(debug) {
//				resp.getWriter().println("Attachment: "+attachment);
//				if(attachment != null) {
//					resp.getWriter().println("Attachment.getTemplate(): "+attachment.getTemplate());
//				}
//			}
//			
//			if (attachment != null && attachment.getTemplate() != null) {
//
//				resp.setContentType((attachment.getTemplate().getContentType() == null || attachment
//						.getTemplate().getContentType().isEmpty()) ? "text/html; charset=utf-8"
//						: attachment.getTemplate().getContentType());
//
//				context.put("page", attachment);
//
//				Velocity.evaluate(context, resp.getWriter(), "log tag name", attachment
//						.getTemplate().getHtmlAsString());
//				return;
//			} else {
//				Matcher matcher = imageWithSize.matcher("/" + url);
//				if (matcher.find()) {
//					String url2 = url.substring(0,
//							url.length() - matcher.group(3).length() - matcher.group(4).length() -
//							matcher.group(5).length()- 3)
//							+ matcher.group(4) + ".html";
//					if(debug) {
//						resp.getWriter().println("Found. Url2: "+url2);
//					}
//					
//					Attachment attachment2 = attachmentDao.findByUrl(url2);
//
//					if(debug) {
//						resp.getWriter().println("Attachment2: "+attachment2);
//						if(attachment2 != null) {
//							resp.getWriter().println("Attachment2.getTemplate(): "+attachment2.getTemplate());
//							resp.getWriter().println("Attachment2.getFileKey(): "+attachment2.getFileKey());
//						}
//					}
//
//					
//					if (attachment2 != null) {
//						
//						ImagesService imagesService = ImagesServiceFactory.getImagesService();
//
//				        Image oldImage = ImagesServiceFactory.makeImageFromBlob(new BlobKey(attachment2.getFileKey()));
//				        Transform resize = ImagesServiceFactory.makeResize(1600, 1600);
//				        int size = Integer.parseInt(matcher.group(3));
//				        if(size <= 1600) {
//				        	if("h".equals(matcher.group(2))) {
//				        		resize = ImagesServiceFactory.makeResize(1600, size);
//				        	} else if("w".equals(matcher.group(2))) {
//				        		resize = ImagesServiceFactory.makeResize(size, 1600);
//				        	} else if("s".equals(matcher.group(2))) {
//				        		resize = ImagesServiceFactory.makeResize(size, size);
//				        	}
//				        }
//
//				        Image newImage = imagesService.applyTransform(resize, oldImage);
//				        if(debug) {
//							resp.getWriter().println("oldImage: "+oldImage);
//							resp.getWriter().println("resize: "+resize);
//							resp.getWriter().println("newImage: "+newImage);
//				        }
//				        
//				        byte[] newImageData = newImage.getImageData();
//
//				        if(!debug) {
//							resp.setContentType("image/jpeg");
//							resp.getOutputStream().write(newImageData);
//				        }
//
//						return;
//					}
//				}
//
//			}
//		}
//		if (page == null || page.getTemplate() == null) {
//			page = pageDao.findByUrl("404");
//			if (page == null || page.getTemplate() == null) {
//				resp.sendError(HttpServletResponse.SC_NOT_FOUND);
//				return;
//			}
//			
//			resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
//
//			resp.setContentType((page.getTemplate().getContentType() == null || page.getTemplate()
//					.getContentType().isEmpty()) ? "text/html; charset=utf-8" : page.getTemplate()
//					.getContentType());
//
//			context.put("page", page);
//
//			Velocity.evaluate(context, resp.getWriter(), "log tag name", page.getTemplate()
//					.getHtmlAsString());
//
//			return;
//		}
//
//		resp.setContentType((page.getTemplate().getContentType() == null || page.getTemplate()
//				.getContentType().isEmpty()) ? "text/html; charset=utf-8" : page.getTemplate()
//				.getContentType());
//
//		context.put("page", page);
//
//		Velocity.evaluate(context, resp.getWriter(), "log tag name", page.getTemplate()
//				.getHtmlAsString());
		
//		resp.setContentType("text/html; charset=utf-8");
		resp.getWriter().println("qwe: "+url);
		

	}

}
